<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 15, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

class SysCache {
	
	/**
	 * Get the output handler
	 *
	 * @return Cache_Lite_Output
	 */
	static function &getOutputInstance($folder = "default", $lifeTime = null) {
		static $instances;
		if (empty($instances)) {
			$instances = array();
		}
		$conf = SystemSite::getSiteConfig ();
		if (empty ( $instances[$folder] )) {
			RLibImporter::file ( 'cache/Lite/Output.php' );
			
			$cacheDir = SYS_ROOT_PATH . "cache/$folder";
			RFileFolderHelper::checkAndCreateFolder($cacheDir);
			$option = array ('caching' => true, 'cacheDir' => $cacheDir."/", 'lifeTime' => $conf->cachetime );
			
			$instances[$folder] = new Cache_Lite_Output ( $option );
		}
		if ($lifeTime <= 1) {
			$lifeTime = $conf->cachetime;
		}
		$cache = &$instances[$folder];
		$cache->setOption('lifeTime', $lifeTime);
		return $cache;
	}
	
	/**
	 * Start cache
	 *
	 * @param string $id
	 * @param string $group
	 */
	static function startCache($id, $group = 'default', $lifeTime = null) {
		if (SystemSite::isCache ()) {
			$out = SysCache::getOutputInstance ($group, $lifeTime);
			return $out->start ( $id, $group );
		}
		return false;
	}
	
	/**
	 * End cache and save or not
	 *
	 * @param boolean $save save content into cache or not
	 */
	static function endCache($group='default', $save = true, $lifeTime = null) {
		if (! SystemSite::isCache()) {
			return;
		}
		
		if (! $save) {
			ob_end_flush ();
			return;
		}
		$out = SysCache::getOutputInstance ($group, $lifeTime);
		return $out->end ();
	}
}
?>